package com.sinothk.server.plus.qrcode;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 一维码
 */
public class GenerateCodeCreator {
//    public static void main(String[] args) throws Exception {
////        generateCode(new File("1dcode.png"), "1390351289", 500, 250);
//
////        String result = readCode(new File("1dcode.png"));
////        System.out.println(result);
//    }

    /**
     * @param file    生成的文件名称
     * @param coreStr 一维码存储的数据信息
     * @return void
     */
    public static void generateCode(File file, String coreStr) {
        generateCode(file, coreStr, 500, 250);
    }

    /**
     * @param file   生成的文件名称
     * @param code   一维码存储的数据信息
     * @param width  生成图片的宽度
     * @param height 生成图片的高度
     * @return void
     */
    public static void generateCode(File file, String code, int width, int height) {
        // 定义位图矩阵BitMatrix
        BitMatrix matrix = null;
        try {
            // 使用code_128格式进行编码生成100*25的条形码
            MultiFormatWriter writer = new MultiFormatWriter();

            matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        // 将位图矩阵BitMatrix保存为图片
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param readImage 读取一维码图片名
     * @return void
     */
    public static String readCode(File readImage) throws Exception {
        BufferedImage image = ImageIO.read(readImage);
        if (image == null) {
            throw new Exception("读取[" + readImage.getPath() + "]失败");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "gbk");
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }
}
